<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Common\Tree;


class CategoryController extends Controller {
/**
分类列表
**/
    public function indexAction(){
        $model = M();
        $sql = "select * from category";
        $cateData = $model->query($sql);


        $tree = new Tree();
        $resdata = $tree->categorytree($cateData);

        $this->assign('cateData',$resdata);


        $this->display('index');

    }
    /**
    添加分类
     **/
    public function addAction(){
        if(IS_POST){
            var_dump($_POST);
            $category_name = I(category_name);
            $fid = I('post.fid');
            $sql = "insert into category(category_name,fid) values('{$category_name}','{$fid}')";
            $model = M();
            $model->execute($sql);
            $this->success('添加成功',U('Admin/category/index'),2);


        }else{
            $model = M();
            $sql = "select * from category";
            $cateData = $model->query($sql);
            $tree = new Tree();
            $resdata = $tree->categorytree($cateData);
	    	$this->assign('cateData',$resdata);

            $this->display('add');
        }
    }
    /**
    修改分类
     **/
    public function editAction(){
        $id = I('get.id');
        var_dump($id);
        $model = M();

        $sql = "select * from category where id='$id'";
        $category_name = $model->query($sql);

        $category_name = $category_name[0];
        $this->assign('category_name',$category_name);
        var_dump($category_name);

//        $model = M();
//        $sql = "select * from category";
//        $cateData = $model->query($sql);
//        $tree = new Tree();
//        $resdata = $tree->categorytree($cateData);
//        $this->assign('cateData',$resdata);

        $this->display('edit');

    }
    public function modifyAction(){
        $category_name = I('post.category_name');
        $id = I('post.id');

        //更新数据库
        $sql = "update category set category_name='$category_name'  where id='{$id}'";
        $model = M();
        $model->execute($sql);
        $this->success('修改成功',U('Admin/category/index'),2);

    }

    /**
    删除分类
     **/
    public function deleteAction(){
        $id = I('get.id');
        $sql = "delete from category where id='{$id}'";
        $model = M();
        $model->execute($sql);
        $this->success('删除成功',U('Admin/Category/index'),2);
    }
}